from rest_framework.generics import GenericAPIView, ListAPIView, RetrieveAPIView
from rest_framework.response import Response

from rest_framework.views import APIView
from rest_framework.viewsets import ReadOnlyModelViewSet

from areas.models import Area
from areas.serializers import AreaViewSerializer, SubsViewSerializer

from rest_framework_extensions.cache.decorators import cache_response
from rest_framework_extensions.cache.mixins import CacheResponseMixin


###########################继承视图集############################

class AreaViewSet(CacheResponseMixin, ReadOnlyModelViewSet):
    '''ReadOnlyModelViewSet提供了listmodelmixin和retrievemodelmixin'''

    # 根据不同的动作,使用不同的序列化器个查询集
    def get_serializer_class(self):

        if self.action == 'list':
            return AreaViewSerializer
        else:
            return SubsViewSerializer

    def get_queryset(self):
        if self.action == 'list':

            return Area.objects.filter(parent=None)

        else:
            return Area.objects.all()

###########################继承子类视图############################
# class AreasView(ListAPIView):
#     # 获取所有省级地区
#     serializer_class = AreaViewSerializer
#     queryset = Area.objects.filter(parent=None)
#
#     # def get(self, request):
#     #     # 获取查询集
#     #     areas = self.get_queryset()
#     #     serializer = self.get_serializer(areas, many=True)
#     #
#     #     return Response(serializer.data)
#
#
# class SubsView(RetrieveAPIView):
#     '''获取指定地区'''
#     serializer_class = SubsViewSerializer
#     queryset = Area.objects.all()
#
#     # def get(self, request, pk):
#     #     # 获取指定地区信息
#     #     # area = Area.objects.get(pk=pk)
#     #
#     #     area = self.get_object()
#     #
#     #     serializer = self.get_serializer(area)
#     #
#     #     return Response(serializer.data)
